<?php

namespace App\Services\Data;

use App\Lib\Util\ErrorCode;
use App\Models\Admin\data\LottoModel;

/**
 * Class DemoService
 * @package App\Services\Admin
 */
class LottoService
{
    private $LottoModel;

    public function __construct(LottoModel $LottoModel)
    {
        $this->LottoModel = $LottoModel;

    }

  /**
   * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
   * @throws \Exception
   */
    public function getLottoList()
    {
      $arrDemo = array(
        'data'=>array(),
        'cedata'=>array(),
        'threeData'=>array(),
        'fourData'=>array(),
        'fiveData'=>array(),
        'sixData'=>array(),
        'sevenData'=>array(),
        'day'=>array()
      );
        $arrLotto = $this->LottoModel->query()->orderBy('no', 'asc')->get();
        if(empty($arrLotto)){
            throw new \Exception('',ErrorCode::NO_DATA);
        }

        $arrDemo = $this->getNumberList($arrLotto,7);
        return $arrDemo;
    }


    private function getNumberList($data,$key = 1)
    {
      $arrData = array(
          'data'=>array(),
          'forecast'=>array(),
          'day'=>array(),
        );
        $ost = '26';
        foreach ($data as $val){
          if(!empty($val->lotteryUnsortDrawresult)){
            $val->lotteryUnsortDrawresult = explode('#',str_replace(array("+"," "),'#',$val->lotteryUnsortDrawresult));
            if(is_array($val->lotteryUnsortDrawresult) && count($val->lotteryUnsortDrawresult) == 7){
              $num = $val->lotteryUnsortDrawresult[$key-1];
              $arrData['forecast'][] = $ost;
              $arrData['data'][] = $num;
              $arrData['day'][] = $val->no;
              //下次的预测值
              $cha = abs($ost - $num);
              $ost = $this->algorithm($ost,$num);
            }

          }
        }
        $arrData['data'][] = '10';
        $arrData['forecast'][] = $ost;
        $arrData['day'][] = '测试';

        //预测数据
        $arrData['ce'] = $ost;
        //最后一期相差值
        $arrData['cha'] = $cha;
        return $arrData;
    }

  /**
   * 17.6
   * @param
   *  $a 平滑常数，其取值范围为[0,1]
   *  $ost 上期取值
   *  $yt 实际值
   */
    public function algorithm($ost,$yt,$a = '0.8')
    {

        $st = $a*$yt+(1-$a)*$ost;
        return $st;
    }


    public function getNumberData()
    {
      $arrLotto = $this->LottoModel->query()->orderBy('no', 'asc')->get()->toArray();
      if(empty($arrLotto)){
        throw new \Exception('',ErrorCode::NO_DATA);
      }
      $arrData = array(
        '1'=>intval($this->getNumber($arrLotto,1)),
        '2'=>intval($this->getNumber($arrLotto,2)),
        '3'=>intval($this->getNumber($arrLotto,3)),
        '4'=>intval($this->getNumber($arrLotto,4)),
        '5'=>intval($this->getNumber($arrLotto,5)),
        '6'=>intval($this->getNumber($arrLotto,6)),
        '7'=>intval($this->getNumber($arrLotto,7))
      );

      return $arrData;
    }


  private function getNumber($data,$key = 1)
  {
    $arrData = array(
      'data'=>array(),
      'forecast'=>array(),
      'day'=>array(),
    );
    $ost = '26';
    foreach ($data as $val){
      if(!empty($val['lotteryUnsortDrawresult'])){
        $val['lotteryUnsortDrawresult'] = explode('#',str_replace(array("+"," "),'#',$val['lotteryUnsortDrawresult']));
        if(is_array($val['lotteryUnsortDrawresult']) && count($val['lotteryUnsortDrawresult']) == 7){
          $num = $val['lotteryUnsortDrawresult'][$key-1];
          //下次的预测值
          $oldOst = $ost;
          $ost = $this->algorithm($ost,$num);
        }
      }
    }
    //预测数据
    $ce = $ost;
    //最后一期相差值
    $cha = abs($oldOst - $num);
//    if(($ce - $cha) > 0){
//      $numS = $ce - $cha;
//    }else{
//      $numS = $ce + $cha;
//    }

    if($ce<=15){
      $numS = $ce + $cha;
    }else{
      $numS = $ce - $cha;
    }
    return $numS;
  }

}
